<template>
  <div>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getGoodsDataList()">
      <el-form-item>
        <el-input v-model="dataForm.goodsName" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="pageIndex = 1
        getGoodsDataList()">查询</el-button>
        <el-button @click="dataForm.goodsName=''
         getGoodsDataList" type="info" plain >重置</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">添加商品</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="商品ID">
      </el-table-column>s
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="商品名称"
        width="400">
        <template slot-scope="scope">
            <div>
              <div style="float: left">
              <img :src="scope.row.img" style="width: 150px; height:100px; margin-top: 26px"/>
              </div>
              <div style="float: left; margin-left: 140px; margin-top: -100px">
              <ul>
                <li style="list-style-type:none;float: left"><a :href="'http://www.yilianbaihui.cn/item_show.html?goodsId=' + scope.row.id">{{scope.row.name}}</a></li>
              </ul>
              </div>
            </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsPrice"
        header-align="center"
        align="center"
        label="价格">
        <template slot-scope="scope">
          <div>
            <p align="left">
              积分价：{{scope.row.integral}}<br/>
              商城售价：{{scope.row.vipPrice}}<br/>
              市场价：{{scope.row.goodsPrice}}<br/>
              型号：{{ scope.row.goodsSerial}}<br/>
            </p>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="img"
        header-align="center"
        align="center"
        label="商品图">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover">
            <img :src="scope.row.img" style="max-width: 300px"/>
            <img slot="reference" :src="scope.row.img" :alt="scope.row.img" style="max-height: 30px;max-width: 80px"/>
          </el-popover>
        </template>
      </el-table-column> -->
      <el-table-column
        prop="goodsCostingPrice"
        header-align="center"
        align="center"
        label="利润">
      </el-table-column>
      <el-table-column
        prop="goodsInventory"
        header-align="center"
        align="center"
        label="库存">
      </el-table-column>
      <el-table-column
        prop="isPromote"
        header-align="center"
        align="center"
        label="起批数量">
      </el-table-column>
      <el-table-column
        prop="combinPrice"
        header-align="center"
        align="center"
        label="现金折扣">
        <template slot-scope="scope">
          <span>{{scope.row.combinPrice === 10?'无折扣':scope.row.combinPrice + ' 折'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="combinIntegral"
        header-align="center"
        align="center"
        label="积分折扣">
        <template slot-scope="scope">
          <span>{{scope.row.combinIntegral === 10?'无折扣':scope.row.combinIntegral + ' 折'}}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <!-- <el-button type="text" size="small" @click="details(scope.row.id)">查看</el-button> -->
          <el-button type="text" size="small" @click="update(scope.row.id,scope.row.isPromote,scope.row.combinPrice,scope.row.combinIntegral)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getGoodsDataList"></add-or-update>

    <el-dialog title="设置起批数据" :visible.sync="dialogVisible" width="30%" v-el-drag-dialog>
      <el-form ref="form" :model="updateBatchForm" label-width="120px" :rules="rules">
        <el-form-item label="设置起批数量：" prop="isPromote">
          <el-input v-model="updateBatchForm.isPromote"  maxlength="7" property="设置起批数量"></el-input>
        </el-form-item>
        <el-form-item label="设置现金折扣：">
          <el-select v-model="updateBatchForm.combinPrice" filterable  placeholder="请选择" >
            <el-option key="无折扣" label="无折扣" :value="10"></el-option>
            <span  v-for="i in 9">
              <el-option
                v-for="(j,index) in 10"
                :key="i+'.'+index"
                :label="i+'.'+index +' 折'"
                :value="i+'.'+index">
              </el-option>
            </span>
            <el-option :key="9.9" label="9.9 折" :value="9.9"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设置积分折扣：">
          <el-select v-model="updateBatchForm.combinIntegral" filterable  placeholder="请选择" >
            <el-option key="无折扣" label="无折扣" :value="10"></el-option>
            <span  v-for="i in 9">
              <el-option
                v-for="(j,index) in 10"
                :key="i+'.'+index"
                :label="i+'.'+index +' 折'"
                :value="i+'.'+index">
              </el-option>
            </span>
            <el-option :key="9.9" label="9.9 折" :value="9.9"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="设置采购分类：" prop="purchaseClassId">
          <el-cascader v-model="updateBatchForm.purchaseClassId" :options="purchaseClassList" :props="props" expand-trigger="hover" filterable></el-cascader>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateData" :disabled="checkFlag">确 定</el-button>
        </span>
    </el-dialog>
    <shoppinggoods-add-or-update v-if="detailsVisible" ref="ShoppinggoodsAddOrUpdate" @refreshDataList="getDataList"></shoppinggoods-add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './goods-add-or-update'
  import ShoppinggoodsAddOrUpdate from '../goods/shoppinggoods-add-or-update'
  export default {
    data () {
      let checkNum = (rule, value, callback) => {
        if (!value) {
          this.checkFlag = true
          return callback(new Error('起批数不能为空'))
        }
        // 判断是否是整数 es6的语法
        if (Math.sign(Number.parseFloat(value)) === -1 || !Number.isInteger(Number.parseFloat(value))) {
          this.checkFlag = true
          return callback(new Error('起批数不合法'))
        }
        this.checkFlag = false
        callback()
      }
      return {
        checkFlag: false,
        detailsVisible: false,
        dialogVisible: false,
        dataForm: {
          goodsName: ''
        },
        updateBatchForm: {
          id: 0,
          isPromote: 0,
          combinPrice: 10,
          combinIntegral: 10
        },
        // props: {
        //   value: 'id',
        //   label: 'className',
        //   children: 'subGoodsClassList'
        // },
        // purchaseClassList: [],
        activityId: 0,
        goodsSkuList: '[]',
        spikeActivityId: null,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        sequenceVisible: false,
        updateForm: {
          updateId: 0,
          updateSequence: '',
          updateSaleNum: ''
        },
        rules: {
          isPromote: [
            { validator: checkNum, trigger: 'blur' }
          ]}
      }
    },
    components: {
      AddOrUpdate,
      ShoppinggoodsAddOrUpdate
    },
    activated () {
      this.getGoodsDataList()
    },
    methods: {
      // 获取数据列表
      getGoodsDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/activity/goods/queryBatchPurchaseGoodsList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex - 1,
            'limit': this.pageSize,
            'key': this.dataForm.goodsName
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      sequenceUpdate () {
        this.$http({
          url: this.$http.adornUrl('/activity/spikeGoods/modify'),
          method: 'post',
          data: this.$http.adornData({
            'id': this.updateForm.updateId,
            'goodsSequence': this.updateForm.updateSequence,
            'goodsSaleNum': this.updateForm.updateSaleNum
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getGoodsDataList(this.activityId)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
          this.sequenceVisible = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getGoodsDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getGoodsDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      details (id) {
        this.detailsVisible = true
        this.$nextTick(() => {
          this.$refs.ShoppinggoodsAddOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/activity/goods/removeBatchPurchaseGoods'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getGoodsDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      update (id, isPromote, combinPrice, combinIntegral) {
        // debugger
        // this.$http({
        //   url: this.$http.adornUrl(`/activity/goods/queryPurchaseClassList`),
        //   method: 'get',
        //   params: this.$http.adornParams()
        // }).then(({data}) => {
        //   if (data && data.code === 0) {
        //     this.purchaseClassList = data.data
        //   }
        // })
        this.dialogVisible = true
        this.updateBatchForm.id = id
        this.updateBatchForm.isPromote = isPromote
        this.updateBatchForm.combinPrice = combinPrice
        this.updateBatchForm.combinIntegral = combinIntegral
        // this.updateBatchForm.purchaseClassId = []
        // this.updateBatchForm.purchaseClassId = [parentId, purchaseClassId]
      },
      updateData () {
        let temp = {}
        temp[this.updateBatchForm.id] = this.updateBatchForm
        let json = JSON.stringify(temp)
        this.$http({
          url: this.$http.adornUrl(`/activity/goods/addBatchPurchaseGoods`),
          method: 'post',
          data: this.$http.adornData(json, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.getGoodsDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
          this.dialogVisible = false
        })
      }
    }
  }
</script>
<style>
  .characters{
    width:300px;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
  }
</style>
